#include "StdAfx.h"
#include "Time.h"
#include <cassert>
#include <ctype.h>

using namespace std;


Time::~Time(void)
{
}

void Time::Set(unsigned int hour, unsigned int minute, char ampm){
	ampm= toupper(ampm);
	assert(hour>=1 && hour <=12 && minute>=0 
		&& minute <=59 && (ampm=='P'|| ampm=='A'));
	this->hour= hour;
	this->minute= minute;
	this->ampm= ampm;
	this->millTime= ToMillTime(hour, minute, ampm);
}

void Time::Display(ostream & out) const{
	out<<hour <<":"<< (minute<10? "0" :"")
		<<minute <<" "<< ampm<< ".M. ("
		<<millTime<< " mil. Time)"<<endl;
}

bool Time::operator<(const Time& other) const{
	return this->millTime<other.millTime;
}

bool Time::operator>(const Time& other) const{
	return this->millTime> other.millTime;
}

bool Time::operator==(const Time& other) const{
	return this->millTime==other.millTime;
} 

unsigned int Time::Hour() const{
	return this->hour;
}

unsigned int Time::Minute() const{
	return this->minute;
}

unsigned int Time::MilTime() const{
	return this->millTime;
}

void Time::ToStandarTime(unsigned int milTime){
	this->hour= milTime/ 100 % 12;
	if(hour==0)
		hour= 12;
	this->minute=milTime %100;
	this->ampm=(milTime/100<12?'A':'P');
}

void Time::Advance(unsigned int hours, unsigned int minutes){
	this->millTime += hours*100+ minutes;
	unsigned int h=this->millTime / 100,
		m=this->millTime % 100;
	h += m / 60;
	m%= 60;
	h%= 24;
	this->millTime = h*100 + m;
	this->ToStandarTime(this->millTime);
}

ostream& operator<<(ostream& out, const Time& t){
	t.Display(out);
	return out;
}

int Time::ToMillTime(unsigned hour, unsigned minute, char ampm){
	if(hour==12 && ampm=='P')
		hour= -12;
	return hour*100+ minute +(ampm=='P'? 1200 :0);
}
